package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageRequestDto;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.model.admin.dtos.ChannelDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-08-07
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    //分页查询频道列表
    @Override
    public PageResult<AdChannel> findByPage(ChannelDto dto) {
        IPage<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(dto.getName()), AdChannel::getName, dto.getName());
        page = this.page(page, wrapper);
        if (CollectionUtils.isEmpty(page.getRecords())) {
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        PageResult<AdChannel> pageResult = new PageResult<>(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());

        return pageResult;
    }

    //新增频道信息
    @Override
    public ResponseResult saveChannel(AdChannel adChannel) {
        adChannel.setCreatedTime(new Date());
        boolean save = this.save(adChannel);
        if (save) {
            return ResponseResult.ok();
        } else {
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
    }

    //修改频道信息
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        if (adChannel.getId() == null) {
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        boolean update = this.updateById(adChannel);
        if (update) {
            return ResponseResult.ok();
        } else {
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
    }

    @Override
    public ResponseResult delChannel(Integer id) {
        AdChannel adChannel = this.getById(id);
        if (adChannel == null) {
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (adChannel.getStatus()){
            throw new LeadException(AppHttpCodeEnum.DATA_CAN_NOT_DEL);
        }
        boolean remove = this.removeById(id);
        if (remove) {
            return ResponseResult.ok();
        } else {
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
    }

    //查询所有频道信息
    @Override
    public ResponseResult findAll() {
        List<AdChannel> list = this.list();
        if (CollectionUtils.isEmpty(list)) {
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.ok(list);
    }
}
